package com.jojo.bubble.common.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


/**
 * @Author JoJo
 * @Data 2021/8/30$ 14:41$
 * @Description
 * @Param $
 * @Return $
 */
@Slf4j
@Configuration
@WebFilter(filterName = "accessFilter", urlPatterns = "/api/*")
public class CorsFilter implements Filter {
    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");
        chain.doFilter(req, res);
    }

    @Override
    public void init(FilterConfig filterConfig) {
        log.info("CorsFilter init！");
    }

    @Override
    public void destroy() {}

//    public static ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(200);
    public static ExecutorService executorService = Executors.newCachedThreadPool();
}
